package com.logic.modular.gameCore.shop.service;

import cn.hutool.core.bean.BeanUtil;
import com.comment.pojo.domain.logic.core.CoreConfig;
import com.comment.pojo.domain.logic.core.CoreProp;
import com.logic.modular.gameCore.config.service.impl.CoreConfigServiceImpl;
import com.logic.modular.gameCore.cultivate.prop.CorePropServiceImpl;
import com.comment.util.pay.WeightDraw;
import lombok.extern.slf4j.Slf4j;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logic.modular.gameCore.shop.mapper.CoreShopGoodsMapper;
import com.comment.pojo.domain.logic.core.CoreShopGoods;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 商店基础数据(CoreShopGoods)表服务实现类
 *
 * @author cxy
 * @since 2024-12-09 13:56:53
 */
@Slf4j
@Service
public class CoreShopGoodsServiceImpl
        extends ServiceImpl<CoreShopGoodsMapper, CoreShopGoods> implements SmartInitializingSingleton {
    private static final Cache<String, Map<Long, CoreShopGoods>> cache = Caffeine.newBuilder().build();

    @Override
    public void afterSingletonsInstantiated() {
        List<CoreShopGoods> list = this.list();
        cache.put("coreShopGoods",
                list.stream().collect(Collectors.toMap(CoreShopGoods::getId, coreShopGoods->{
                    coreShopGoods.init();
                    Long propId = coreShopGoods.getPropId();
                    CoreProp coreProp = CorePropServiceImpl.getCache(propId);
                    coreShopGoods.setName(coreProp.getName());
                    coreShopGoods.setQuality(coreProp.getQuality());
                    return coreShopGoods;
                }))
        );
        log.info("商店基础数据缓存完成,数据量为:{}", list.size());
    }

    public static boolean checkSize(int size){
        return size == listCache().size();
    }

    public static List<CoreShopGoods> listCache() {
        return new ArrayList<>(mapCache().values());
    }

    public static Map<Long, CoreShopGoods> mapCache() {
        Map<Long, CoreShopGoods> values = cache.getIfPresent("coreShopGoods");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values;
    }

    public static CoreShopGoods getCache(Long id) {
        return mapCache().get(id);
    }

    public static Set<CoreShopGoods> obtainEquip() {
        Map<Long, CoreShopGoods> longCoreShopGoodsMap = mapCache();
        Map<Long, CoreShopGoods> expand = new HashMap<>();
        for (CoreShopGoods value : longCoreShopGoodsMap.values()) {
            if (value.getType() == 4) {
                long expandId = Math.multiplyExact(value.getPropId(), 20);
                CoreShopGoods bean = BeanUtil.toBean(value, CoreShopGoods.class);
                bean.setId(expandId);
                expand.put(expandId, bean);
            }
        }

        List<WeightDraw.Prize> prizes = expand.values().stream()
                .map(item -> new WeightDraw.Prize(item.getId(), item.getWeight()))
                .collect(Collectors.toList());


        WeightDraw draw = new WeightDraw(prizes);


        // 商店装备刷新规则: 总量#1个 ,品质5(最多#2件),品质4(最多#3件)	12	2	4
        CoreConfig coreConfig = CoreConfigServiceImpl.getCache(10L);

        int index = 0;
        int maxCount = Integer.parseInt(coreConfig.getValue1().toString());

        int index5 = 0;
        int max5Count = Integer.parseInt(coreConfig.getValue2());

        int index4 = 0;
        int max4Count = Integer.parseInt(coreConfig.getValue3());

        Set<Long> logId = new HashSet<>();
        Set<CoreShopGoods> back = new HashSet<>();
        // 随机数
        while (index < maxCount) {
            WeightDraw.Prize prize = draw.drawCard();
            CoreShopGoods coreShopGoods = expand.get(prize.getId());

            if (coreShopGoods.getQuality() == null) {
                continue;
            }
            if (coreShopGoods.getQuality() == 5) {
                if (max5Count > index5) {
                    index5++;
                } else {
                    continue;
                }
            }
            if (coreShopGoods.getQuality() == 4) {
                if (index4 < max4Count) {
                    index4++;
                } else {
                    continue;
                }
            }

            Long id = coreShopGoods.getId();
            while (logId.contains(id)) {
                id++;
            }
            logId.add(id);

            CoreShopGoods bean = BeanUtil.toBean(coreShopGoods, CoreShopGoods.class);
            bean.setId(id);
            back.add(bean);

            index++;
        }
        System.out.println(back.stream().map(CoreShopGoods::getId).collect(Collectors.toList()));
        return back;
    }


}